<?php
/*
*	created by cbq in 2017.02.08
*	系统日志模型类
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Userinfo_m extends MY_Model {
    public function __construct()
    {
        parent::__construct();
        $this->table='userinfo';
    }
     //文章ajax 分页
    public function ajaxall($where=array(),$field="*",$order='id asc'){
        $pagesize=getConfig('pagesize');
        $this->com_where($where);
        $this->db->select('count(id) as counts');
        $query=$this->db->get($this->table);
        $counts=$query->row_array();

        $page=isset($_REQUEST['page'])?$_REQUEST['page']:1;
        $startRow=$pagesize * ($page - 1);
        $this->com_where($where);
        $this->db->select($field);
        $this->db->order_by($order);
        $this->db->limit($pagesize,$startRow);
        $query2=$this->db->get($this->table);
        $data['list']=$query2->result_array();
        $data['pageshow']=$this->pageajax_show($pagesize,$counts['counts']);
        return $data;
    }
    //查询所有
    public function selall($where=array(),$field="*"){
        $this->com_where($where);
        $this->db->select($field);
        $query=$this->db->get($this->table);
        $data=$query->result_array();
        return $data;

    }
    //查询单条用户信息
    public function sel($where=array(),$field="*"){
    	  $this->com_where($where);
    	  $this->db->select($field);
      	$query=$this->db->get($this->table);
     	  $data=$query->row_array();
      	return $data;

    }
    //添加管理员
    public function add($data){
      $arr=array();
      $arr['name']=$data['name'];
      $arr['showname']=$data['showname'];
      $arr['pass']=passcreate($data['pass']);
      $arr['userface']=$data['userface'];
      $arr['sex']=$data['sex'];
      $arr['mobile']=$data['mobile'];
      $arr['remarks']=isset($data['remarks'])?$data['remarks']:'';
      $arr['qq']=$data['qq'];
      $arr['status']=$data['status'];
      $arr['addtime']=time();
      $this->db->insert($this->table,$arr);
      return $this->db->insert_id();
    }
    //编辑管理员信息
    public function edit($where,$data){
      if(empty($where)){return false;}
      $this->com_where($where);
      $this->db->update($this->table,$data);
      return $this->db->affected_rows();
    }
    //修改用户锁定状态
  	public function login_lock_type($id,$type){
  		  $where['id']=array('=',$id);
  		  $data['locked']=$type;
      	$this->com_where($where);
      	$this->db->update($this->table,$data);
      	return $this->db->affected_rows();
  	}
    //更新用户登陆成功后的信息
    public function login_edit($editdata){
        $where['id']=array('=',$editdata['id']);
        $data['online']=1;
        if(cms_is_mobile()){
          $lastloginfrom="app";
        }else{
          $lastloginfrom='web';
        }
        $data['lastlogintime']=time();
        $data['lastloginfrom']=$lastloginfrom;
        $data['lastloginip']=get_client_ip();
        $data['logintimes']='logintimes+1';
        $data['usertoken']=$editdata['usertoken'];
        $this->com_where($where);
        $this->db->update($this->table,$data);
        return $this->db->affected_rows();
    }
    //登出时 更新管理员信息
    public function loginout_edit($id){
        $where['id']=array('=',$id);
        $data['online']=2;
        $data['lastlogouttime']=time();
        $data['usertoken']='';
        $this->com_where($where);
        $this->db->update($this->table,$data);
        return $this->db->affected_rows();
    }
  
}
?>
